using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;


public partial class Pages_StockStatement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox_fromdate.Text = DateTime.Today.Year.ToString();
        TextBox_todate.Text = DateTime.Today.ToString();

    }
    protected void WebUserControlstockselect1_Load(object sender, EventArgs e)
    {
        //TextBox1.Text = Common.stkCodeValue.ToString();
    }
    protected void Button_Statement_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        try
        {
            if (Common.stkCodeValue > 0)  // it means an item has been selected
            {
                TextBox1.Text = " ";

                TextBox_fromdate.Text = "01/01/" + DropDownList3.SelectedValue.ToString(); // prepare a start date from the year entered

                string cmd1 = "select date,nature,refn,qty, cost, w_ton from memo where date>='" +
                    TextBox_fromdate.Text.Trim() + "' and date<='" +
                    TextBox_todate.Text.Trim() + "' and itype=" +
                    Common.stkTypeValue.ToString() + " and icode=" +
                    Common.stkCodeValue.ToString() + " order by date";


                DataSet ds = new DataSet();

                Common.Getdata(ds, cmd1, "SD");


                ds.Tables["SD"].Columns.Add("Particulars", typeof(string));
                ds.Tables["SD"].Columns.Add("DateDMY", typeof(string));
                ds.Tables["SD"].Columns.Add("Voucher", typeof(string));
                ds.Tables["SD"].Columns.Add("ReceiptTon", typeof(decimal));
                ds.Tables["SD"].Columns.Add("IssueTon", typeof(decimal));
                ds.Tables["SD"].Columns.Add("BalanceTon", typeof(decimal));
                ds.Tables["SD"].Columns.Add("Cost", typeof(decimal));

                int i;


            //    ds.Tables["SD"].Rows[0]["date1"] = Convert.ToDateTime(ds.Tables["SD"].Rows[0]["date"]).ToString("dd/MM/yyyy");
                // Calculate and Update balance field

               // date,nature,refn,qty, cost, w_ton

                for (i = 0; i < ds.Tables["SD"].Rows.Count; i++)
                {
                    ds.Tables["SD"].Rows[i]["dateDMY"] = Convert.ToDateTime(ds.Tables["SD"].Rows[i]["date"]).ToString("dd/MM/yyyy");
                    ds.Tables["SD"].Rows[i]["Voucher"] = (ds.Tables["SD"].Rows[i]["nature"]).ToString() + " " + (ds.Tables["SD"].Rows[i]["refn"]).ToString();

                    if (Convert.ToDecimal(ds.Tables["SD"].Rows[i]["qty"]) + Convert.ToDecimal(ds.Tables["SD"].Rows[i]["w_ton"]) < 0)
                        ds.Tables["SD"].Rows[i]["IssueTon"] = Convert.ToDecimal(ds.Tables["SD"].Rows[i]["qty"]) + Convert.ToDecimal(ds.Tables["SD"].Rows[i]["w_ton"]) * (-1);
                    else
                        ds.Tables["SD"].Rows[i]["ReceiptTon"] = Convert.ToDecimal(ds.Tables["SD"].Rows[i]["qty"]) + Convert.ToDecimal(ds.Tables["SD"].Rows[i]["w_ton"]);
                    //  ds.Tables["SD"].Rows[i]["date"] = Convert.ToDecimal(ds.Tables["SD"].Rows[i - 1]["balance"]) - Convert.ToDecimal(ds.Tables["SD"].Rows[i]["credit"]) + Convert.ToDecimal(ds.Tables["SD"].Rows[i]["debit"]);
                    //ds.Tables["SD"].Rows[i]["date1"] = Convert.ToDateTime(ds.Tables["SD"].Rows[i]["date"]).ToString("dd/MM/yyyy");
                }
     



                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();

            }
        }
        catch (Exception Ex)
        {
            TextBox1.Text = "Wrong Input of date(possibly)";

        }
    }
}
